<?php
/**
 * MerchantForm class.
 * MerchantForm is the data structure for keeping merchant account data.
 * It is used by the 'account', 'profile' action of 'MerchantController'.
 * 
 * Yawii 版权所有 @ 2012
 */
class MerchantForm extends CActiveRecord
{
	public $new_email;
	public $new_password;
	public $new_password_repeat;
	
	private $_identity;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return ShopProfile the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return 'tbl_merchant_account';
	}

	/**
	 * Declares the validation rules.
	 * The rules state that password, new_password, new_password_repeat is required,
	 * and password needs to be authenticated.
	 */
	public function rules()
	{
		return array(
			array('password, new_password, new_password_repeat', 'length', 'min'=>4, 'max'=>40),
			//website needs to be http://
			array('url_site', 'url', 'defaultScheme' => 'http'),
			//array('phone', 'length', 'min'=>7, 'max'=>18),
			
			// register event
			array('name, phone, email, category_key, address', 'required', 'on'=>'register', 'message'=>BUtils::T('cannot_be_blank_msg')),
			
			// account event
			array('email, password, new_password, new_password_repeat', 'required', 'on'=>'account', 'message'=>BUtils::T('cannot_be_blank_msg')),
			array('new_password_repeat', 'compare', 'compareAttribute'=>'new_password', 'on'=>'account'),
			array('password', 'authenticate', 'on'=>'account'),
			array('email, new_email', 'email', 'on'=>'account', 'allowEmpty'=>false, 'checkMX'=>true, 'message'=>BUtils::T('email_address_error_msg')),
			
			// profile event
			array('name, address, city', 'required', 'on'=>'profile', 'message'=>BUtils::T('cannot_be_blank_msg')),
			
			array('url_logo', 'file', 'allowEmpty'=>true, 'types'=>'jpg, gif, png'),
			array('name, address, postcode, phone, city, country_code, biz_area, currency_code, category_key, latitude, longitude', 'safe'),
		);
	}

	/**
	 * Declares attribute labels.
	 */
	public function attributeLabels()
	{
		return array(
			'email' => BUtils::T('email_address_lbl'),
			'new_password' => BUtils::T('password_new_lbl'),
			'new_password_repeat' => BUtils::T('password_new_repeat_lbl'),
			'password' => BUtils::T('password_current_lbl'),
			'name' => BUtils::T('company_name_lbl'),
			'url_logo' => BUtils::T('company_logo_lbl'),
			'url_offer_img' => BUtils::T('special_offer_picture_lbl'),
			'address' => BUtils::T('street_address_lbl'),
			'postcode' => BUtils::T('postal_code_lbl'),
			'city' => BUtils::T('city_lbl'),
			'country_code' => BUtils::T('country_lbl'),
			'phone' => BUtils::T('phone_number_lbl'),
			'category_key' => BUtils::T('category_lbl'),
			'url_site' => BUtils::T('web_address_lbl'),
			'opening' => BUtils::T('opening_hours_lbl'),
			'brief' => BUtils::T('brief_lbl'),
			'average_price' => BUtils::T('average_price_lbl'),
			'biz_area' => BUtils::T('biz_area_lbl'),
		);
	}

	/**
	 * Authenticates the password.
	 * This is the 'authenticate' validator as declared in rules().
	 */
	public function authenticate($attribute, $params)
	{
		if (!$this->hasErrors()) {
			$this->_identity = new UserIdentity($this->email, $this->password);
			if (!$this->_identity->authenticate())
				$this->addError('password', BUtils::T('password_old_incorrect_msg'));
		}
		return false;
	}
	
	/**
	 * Authenticates the email.
	 * This is the 'authenticate' validator as declared in rules().
	 */
	public function checkEmail()
	{
		//validate if email exists
		$connection = Yii::app()->db;
		$command = $connection->createCommand('SELECT id FROM tbl_merchant_account WHERE email=:email');
		$row = $command->queryRow(true, array('email' => $this->email));
		$connection->active = false;
		if (empty($row)) {
			return true;
		} else {
			$this->addError('email', BUtils::T('duplicate_email_error_msg'));
			return false;
		}
	}
}
